﻿    function e(id) 
    {
      return document.getElementById(id);
    }
    
    function getWindowHeight() 
    {
      if (window.self && self.innerHeight) 
        {return self.innerHeight;}
      if (document.documentElement && document.documentElement.clientHeight) 
        {return document.documentElement.clientHeight;}
      return 0;
    }
      
    function resizeApp() 
    {
      var offsetTop = 0;
      var mapElem = e("map_canvas");
      for (var elem = mapElem; elem; elem = elem.offsetParent) 
      {
        offsetTop += elem.offsetTop;
      }
      var height = getWindowHeight() - offsetTop - 30;
      if (height >= 0) 
      {
        mapElem.style.height = height + "px";
//        e("wpanel").style.height =e("panel").style.height = (height + 4) + "px";
//        e("paneltoggle").style.paddingTop = (height/2) + "px";
//        e("paneltoggle").style.paddingBottom = (height/2) + "px";
//        var sp = e("spsizer");sp.style.height = Math.max(0, height - sp.offsetTop) + "px";
      }
    }

    //Google maps object
    var map = null;  
    var markerToMove = null; 
    
    //Markers array
    var gMarkers = new Array();
    
    //Initialize google maps control
    function initialize() {
      resizeApp();
      if (GBrowserIsCompatible()) {
          map = new GMap2(document.getElementById("map_canvas"));
          //map.enableGoogleBar();
          map.enableScrollWheelZoom();
          map.addControl(new GScaleControl());
          map.addControl(new GLargeMapControl());
          map.addControl(new GHierarchicalMapTypeControl());
          map.addControl(new GOverviewMapControl());
          map.setCenter(new GLatLng(54.6, 39.6), 11);
          //map.addOverlay(new GMarker(new GLatLng(39, 54),{ title: '123'}));
      }
      LoadMarkers();
    }
    
    
    //Load markers
    function LoadMarkers() {
       var gameID = e("ddlGames").value;
      //Get last markers positions 
       ret = MonitoringServer.AJAXService.GetPoints(gameID, MarkersRecieved);
       //setTimeout("LoadMarkers()",document.getElementById('ddlDelay').value);
    }
    
     //Process markers array
     function MarkersRecieved(markers)
     {
      //Go over the markers recieved from server
      for(i = 0; i < markers.length; i++)
      {
        var marker = markers[i];
        var curGMarker = null;
        //Sys.Debug.trace(marker);
        exists = false;
        //Go over the gmarkers on page
        for(j=0; j <gMarkers.length; j++ )
        {
          var gMarker = gMarkers[j];
          if(gMarker.Marker.ID == marker.ID)
          {
            exists = true;
            gMarker.Marker = marker;
            curGMarker = gMarker;
            break;
          }
        }
        if(!exists)//Create new marker
        {
          gMarkers.push(GreateGMarker(marker));
        }
        else//Move existing marker
        {
          curGMarker.setLatLng(new GLatLng(marker.Latitude, marker.Longitude));
        }
      }
     }

     //Create GMarker using marker data 
     function GreateGMarker(marker)
     {
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = "";//"http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(40, 40);
//        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.shadowSize = new GSize(0, 0);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
       // Create a lettered icon for this point using our icon class
//       var index = marker.ID%26;
//       var letter =  String.fromCharCode("A".charCodeAt(0) + index);
       var letteredIcon = new GIcon(baseIcon);
       letteredIcon.image = "http://localhost/MonitoringServer/photo.aspx?id=1";

        // Set up our GMarkerOptions object
        markerOptions = { icon:letteredIcon };
        //create structure of marker parameters 
        options = {icon:letteredIcon, title : marker.MarkerName, draggable : true,clickable : true};
        //create marker
        var gMarker = new GMarker(new GLatLng(marker.Latitude, marker.Longitude),options);
        gMarker.Marker = marker;
        map.addOverlay(gMarker);
        //add event handlers
        GEvent.addListener(gMarker, "dragend",function(){MoveMarker(gMarker)});
        GEvent.addListener(gMarker, "dragstart",function(){map.closeInfoWindow();});
        GEvent.addListener(gMarker, "click",function(){ShowInfo(gMarker);});
        return gMarker;
     }

     
     //Move marker to current gMarker position 
     function MoveMarker(gMarker)
     {
      //alert(gMarker.getTitle());
       //ret = MonitoringServer.AJAXService.AddPoint(gMarker.getTitle(), gMarker.getLatLng().lat(), gMarker.getLatLng().lng(),null)
     }
     
     //Show HTML info window with information about marker
     function ShowInfo(gMarker)
     {
      var info = MonitoringServer.AJAXService.GetMarkerDescription(gMarker.Marker.ID,MarkerDescriptionRecieved,MarkerDescriptionRecieved,gMarker);
     }
     
     function MarkerDescriptionRecieved (info,gMarker)
     {
      gMarker.openInfoWindowHtml(info);
     }
